package cn.itcast.order.ui;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import cn.itcast.order.R;
import cn.itcast.order.Tools.DataDB;
import cn.itcast.order.Tools.DataTools;
import cn.itcast.order.Tools.Dish;
import cn.itcast.order.Tools.Dish2Adapter;

public class DishActivity extends AppCompatActivity {

    DataDB db;
    DataTools d_tools;
    List<Dish> list = new ArrayList<>();
    ListView listView;
    Dish2Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dish);
        init();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(DishActivity.this,ShowActivity.class);
                intent.putExtra("info",list.get(i).message);
                intent.putExtra("image",list.get(i).image);
                startActivity(intent);
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i1, long l) {
                AlertDialog dialog = new AlertDialog.Builder(DishActivity.this)
                        .setTitle("删除")
                        .setMessage("是否将次移除菜单")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                d_tools.delete(LoginActivity.account,list.get(i1).name);
                                list = d_tools.select(LoginActivity.account);
                                adapter = new Dish2Adapter(DishActivity.this,R.layout.item_dish2,list);
                                listView.setAdapter(adapter);
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        }).show();
                return true;
            }
        });
    }

    private void init(){
        db = new DataDB(DishActivity.this,"Data.db",null,1);
        d_tools = new DataTools(db);
        list = d_tools.select(LoginActivity.account);
        listView = findViewById(R.id.list);
        adapter = new Dish2Adapter(DishActivity.this,R.layout.item_dish2,list);
        listView.setAdapter(adapter);
    }
}